package com.tutou.ikgroup.controller;

import com.tutou.ikgroup.pojo.dto.ChatDTO;
import com.tutou.ikgroup.service.UserChatService;
import com.tutou.ikuser.pojo.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.tutou.ikuser.util.RedisConstants.LOGIN_TOKEN_KEY;


@RestController
@RequestMapping("/chat")
public class UserChatController {
    @Autowired
    private UserChatService userChatService;

    @PostMapping("/OBO")
    public Result chatOBO(@RequestHeader("Authorization") String token, @RequestBody ChatDTO chatDTO) {
        return userChatService.obo(LOGIN_TOKEN_KEY + token, chatDTO);
    }


    @PostMapping("/createChatroom")
    public Result createChatroom(@RequestHeader("Authorization") String token, @RequestBody String chatRoomName) {
        return userChatService.creatChatroom(LOGIN_TOKEN_KEY + token, chatRoomName);
    }

    @PostMapping("/inviteUser")
    public Result inviteUser(@RequestHeader("Authorization") String token, @RequestBody ChatDTO chatDTO) {
        return userChatService.inviteUser(LOGIN_TOKEN_KEY + token, chatDTO);
    }

    @PostMapping("/OBM")
    public Result chatOBM(@RequestHeader("Authorization") String token, @RequestBody ChatDTO chatDTO) {
        return userChatService.obm(LOGIN_TOKEN_KEY + token, chatDTO);
    }

    @PostMapping("/OBOMessage")
    public Result findOBOMessage(@RequestHeader("Authorization") String token, @RequestBody ChatDTO chatDTO) {
        return userChatService.findOBOMessage(LOGIN_TOKEN_KEY + token, chatDTO);
    }

    @PostMapping("/OBMMessage")
    public Result findOBMMessage(@RequestHeader("Authorization") String token, @RequestBody String chatroomId) {
        return userChatService.findOBMMessage(LOGIN_TOKEN_KEY + token, chatroomId);
    }

    //TODO 解散群聊未实现，需要在message添加type字段作为信息类型判断，才可以完成消息的删除
    @DeleteMapping("/deleteChatroom")
    public Result deleteChatroom(@RequestHeader("Authorization") String token, @RequestBody ChatDTO chatDTO) {
        return userChatService.deleteChatroom(token, chatDTO);
    }

    //TODO 退出群聊未实现，需要校验用户是否为群主，如果为群主则直接解散群聊
    @DeleteMapping("/exitChatroom")
    public Result exitChatroom(@RequestBody ChatDTO chatDTO) {
        return userChatService.exitChatroom(chatDTO);
    }

    @GetMapping("/chatroomMember/{chatroomId}")
    public Result chatroomMember(@PathVariable("chatroomId") String chatroomId) {
        return userChatService.getChatroomMember(chatroomId);
    }

    @GetMapping("/friendList/{userId}")
    public Result getFriendList(@RequestParam("userId") String userId) {
        return userChatService.getFriendList(userId);
    }
}
